{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "import json\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../stories/all_stories.yml'\n",
    "with open(path, 'r', encoding='utf-8') as f:\n",
    "    story = yaml.load(f.read(),Loader=yaml.Loader)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def construct_story_map(dataset):\n",
    "    # 由story构建story_map, 根据intent和slot值在story中进行查找来判断action\n",
    "    story_map = []\n",
    "    story_listmap = []\n",
    "    target = 0 \n",
    "    for val in dataset:\n",
    "        data = val['steps']\n",
    "        for index in range(0, len(data), 2):\n",
    "            slots = []\n",
    "            user_intent = []\n",
    "            \n",
    "            # 当前状态\n",
    "            current_intent  =  data[index]['intent']\n",
    "            if 'entities' in data[index].keys():\n",
    "                current_slot = data[index]['entities']\n",
    "            else:\n",
    "                current_slot = []\n",
    "            current_action = [data[index+1]['action']]\n",
    "            \n",
    "            \n",
    "            # 历史状态\n",
    "            pre_intent = []\n",
    "            pre_slot = []\n",
    "            pre_action = []\n",
    "            \n",
    "            pre_history_1 = index-2\n",
    "            if pre_history_1 >= 0:\n",
    "                pre_intent = data[pre_history_1]['intent']\n",
    "                if 'entities' in data[pre_history_1].keys():\n",
    "                    pre_slot = data[pre_history_1]['entities']\n",
    "                    \n",
    "            if pre_action == []:\n",
    "                pre_action = ['PAD']\n",
    "            \n",
    "            slots_sum = [current_slot, pre_slot]\n",
    "            \n",
    "            for i in slots_sum:\n",
    "                if i  != [] and i is not None:\n",
    "                    for val in i:\n",
    "                        for key, j in val.items():\n",
    "                            if key not in slots:\n",
    "                                slots.append((key))\n",
    "            if slots == []:\n",
    "                slots = ['PAD']\n",
    "                \n",
    "            \n",
    "            user_intent_sum = [current_intent, pre_intent]\n",
    "            for i in user_intent_sum:\n",
    "                if i != []:\n",
    "                    user_intent.append(i)\n",
    "            if user_intent == []:\n",
    "                user_intent = ['PAD']\n",
    "            \n",
    "            story_name = \"story\"+str(target)\n",
    "            if {'storys':[slots,user_intent],'action':current_action} in story_map:\n",
    "                pass\n",
    "            else:\n",
    "                story_map.append({'storys':[slots,user_intent],'action':current_action})\n",
    "            story_listmap.append([user_intent,slots,current_action])\n",
    "    return story_map, story_listmap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "story_map, story_listmap =  construct_story_map(story)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../stories/stories_map.json', mode='w', encoding='utf-8') as f:\n",
    "    json.dump(story_map, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../stories/stories_map.json', mode='r', encoding='utf-8') as f:\n",
    "    stories = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
